# name : proc univariate+tranpose (character)
# key : proc.univariate.transpose.character
# contributor: Shuguang Sun
# --
/* Request statistics on the OUTPUT statement. */
/* Use the AUTONAME option to create variable names in the form: */
/* variable_statistic */
/* wide-to-long first, and/or Total */
proc univariate data=${1:dtin} noprint;
  format ${2:group} $ogroup.;
  class $2 (missing order = formatted);
  var ${3:var};
  by ${4:avisit};
  output out = ${5:_out1} n=n mean=mean std=std median=median q1=q1 q3=q3 min=min max=max/ autoname;
  where ;
run;

/* Total */
data $5;
  set $5;
  _bn      = put(n, 18.-l);
  if missing(std) then _bmean = strip(put(mean, 18.2));
  else _bmean = strip(put(mean, 18.2)) || " (" || strip(put(std, 18.2)) || ")";
  _bmedian = put(median, 18.2-l);
  _bq13    = strip(put(q1, 18.2)) || " - " || strip(put(q3, 18.2));
  _brange  = strip(put(min, 18.2)) || " - " || strip(put(max, 18.2));
run;

proc sort data=$5;
  by $4 $2;
run;

/* Transpose the data set so that each statistic becomes an observation. */
proc transpose data=$5 out=${6:dout} (rename = (_name_ = stat)) prefix=_;
  format $2 $group.;
  var _b:;
  id $2;
  by $4;
run;

proc print data=$6 noobs;
  var $4 stat _b:;
  title "$0";
run;
